//
//  WlwLogInController.m
//  HouseWork
//
//  Created by @"ZL" on 16/7/8.
//  Copyright © 2016年 wlw. All rights reserved.
//

#import "WlwLogInController.h"
#import "WlwLocationManager.h"
#import "WlwFormViewWithLeftName.h"
#import "UserLoginRequest.h"
#import "ZLAbstractModel.h"
#import "UserModel.h"
#import "AFNetworking.h"

@interface WlwLogInController()
//账号
@property (nonatomic, strong) WlwFormViewWithLeftName *account;
//密码
@property (nonatomic, strong) WlwFormViewWithLeftName *passwd;
@end

@implementation WlwLogInController

-(void)argumentForCanvas:(id)argumentData{
    if ([argumentData isKindOfClass:[NSString class]]) {
        [self.view makeToast:argumentData];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setNavgationTitle:@"车调宝"];
   
    _account = [[WlwFormViewWithLeftName alloc] initWithLeftName:@"账号" rightImage:nil];
   // _account.textfiled.keyboardType = UIKeyboardTypePhonePad;
    [_account setTextPlaceHold:@"请输入账户名" textMaxLength:11];
    [self.view addSubview:_account];
    [_account mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.equalTo(@44);
    }];
    UIView *accountSeperateView = [[UIView alloc] init];
    accountSeperateView.backgroundColor = DIVIDER_COLOR;
    [self.view addSubview:accountSeperateView];
    [accountSeperateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(10);
        make.top.equalTo(_account.mas_bottom);
        make.right.equalTo(self.view).offset(-10);
        make.height.equalTo(@(1/[ZLHelp deviceScale]));
    }];
    
    _passwd = [[WlwFormViewWithLeftName alloc] initWithLeftName:@"密码" rightImage:nil];
    [_passwd.textfiled setSecureTextEntry:YES];
    [_passwd setTextPlaceHold:@"请输入密码" textMaxLength:30];
    [self.view addSubview:_passwd];
    [_passwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(accountSeperateView.mas_bottom);
        make.height.equalTo(@44);
    }];
    
    UIView *passwdSeperateView = [[UIView alloc] init];
    passwdSeperateView.backgroundColor = DIVIDER_COLOR;
    [self.view addSubview:passwdSeperateView];
    [passwdSeperateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(_passwd.mas_bottom);
        make.right.equalTo(self.view);
        make.height.equalTo(@(1/[ZLHelp deviceScale]));
    }];
    
    
    UIButton *lonInBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    lonInBtn.backgroundColor = RGBACOLOR(0x00, 0xc1, 0x5c, 1.0);
    [lonInBtn addTarget:self action:@selector(logIn) forControlEvents:UIControlEventTouchUpInside];
    [lonInBtn setTitle:@"登 录" forState:UIControlStateNormal];
    lonInBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    [lonInBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    lonInBtn.layer.masksToBounds = YES;
    lonInBtn.layer.cornerRadius = 6.f;
    [self.view addSubview:lonInBtn];
    [lonInBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(passwdSeperateView.mas_bottom).offset(25);
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
        make.height.equalTo(@44);
    }];
}



-(void)dissMissVc
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];

    [[NSNotificationCenter defaultCenter] postNotificationName:GOTOHOMEVC object:nil];
}

#pragma  mark _logIn

-(void)logIn
{
    if (_account.textfiled.text.length == 0) {
        [self.view makeToast:@"账号不能为空"];
        return;
    }
//    if(![ZLHelp isValidateMobile:_account.textfiled.text]){
//        [self.view makeToast:@"请输入正确的手机号格式"];
//        return;
//    }
    if (_passwd.textfiled.text.length == 0) {
        [self.view makeToast:@"密码不能为空"];
        return;
    }
    UserLoginRequest *request = [[UserLoginRequest alloc] initWithModelClass:[UserModel class] completeRequestBlock:^(id sender, NSString *msg, NSError *error) {
        if (error) {
            [self.view makeToast:msg];
            return;
        }
        UserModel *data = sender;
        [[ZLAppData shareAppData] saveUser:data];
//        [self dismissViewControllerAnimated:YES completion:^{
//            
//        }];
        [[NSNotificationCenter defaultCenter] postNotificationName:LOGINSTATUS object:@YES];
       
    }];
    request.user = _account.textfiled.text;
    request.password = _passwd.textfiled.text;
    ZLRequestOperationManager *manager = [[ZLRequestOperationManager alloc] init];
    [manager performWithRequest:request View:self.view];
    
}


@end
